package com.example.userprovider.feign;

import com.example.userapi.feign.IUserFeign;
import com.example.userprovider.utils.JwtGeneratorUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.apache.commons.lang.StringUtils;
import org.example.api.R;
import org.example.exception.ValidException;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @date 2021-4-19 10:16
 */
@RestController
public class AuthUserFeignClient implements IUserFeign {
    @Override
    public R<String> validToken(String token) {
        if(StringUtils.isBlank(token)) {
            throw new ValidException("token为空");
        }
        try {
            Claims claims = JwtGeneratorUtils.parseToken(token);
            return new R.Builder().setData(claims.get("uid").toString()).buildOk();
        }catch (ExpiredJwtException e) {
            return new R.Builder().buildCustomize("token已经过期");
        }catch (SignatureException e) {
            return new R.Builder().buildCustomize("签名校验失败");
        }
    }
}
